var msgbox={
    type:"",
    lang:"zh",
    callback:"",
    color:{
        "error":"orange",
        "confirm":"blue",
        "info":"green"
    },
    zh:{
        "error":"错误",
        "confirm":"请确认",
        "info":"信息"
    },
    Loading:function(msg){
        msgbox.type="loading";
        if(msgbox.lang=="zh"){
            msgbox.CreateBox(msg?msg:"载入中");
        }else{
            msgbox.CreateBox(msg?msg:"Loading...");
        }
    },
    Error:function(msg){
        msgbox.type="error";
        msgbox.CreateBox(msg);
    },
    Info:function(msg){
        msgbox.type="info";
        msgbox.CreateBox(msg);
    },
    Confirm:function(msg){
        msgbox.type="confirm";
        msgbox.CreateBox(msg);
    },
    //关闭msgbox
    CloseBox:function(){
        if(document.querySelector("#global_msgbox")){
            document.body.removeChild(document.querySelector("#global_msgbox"));
            if(msgbox.callback!="" && typeof msgbox.callback=="function"){
                msgbox.callback();
                msgbox.callback="";
            }
        }
    },
    //創建顯示標簽
    CreateBox:function(msg){
        var page=document.createElement("div");
        page.setAttribute("id","global_msgbox");
        page.style.position="fixed";
        page.style.width="100%";
        page.style.height="100vh";
        page.style.backgroundColor="rgba(80,80,80,0.3)";
        page.style.top="0";
        page.style.left="0";
        page.style.zIndex="1000";
        var box=document.createElement("div");
        box.setAttribute("id","global_box");
        box.style.position="fixed";
        box.style.textAlign="center";
        box.style.zIndex="1000";
        if(msgbox.type=="loading"){
            box.style.width="200px";
            box.style.height="60px";
            box.style.lineHeight="60px";
            box.style.marginTop="-30px";
            box.style.marginLeft="-100px";
            box.innerText=msg;
        }else{
            box.style.width="300px";
            box.style.height="200px";
            box.style.marginLeft="-150px";
            box.style.marginTop="-100px";
        }
        box.style.top="50%";
        box.style.left="50%";
        box.style.backgroundColor="white";
        box.style.boxShadow="0 0 5px white";
        box.style.overflow="hidden";

        if(msgbox.type!="loading"){
            var box_title=document.createElement("div");
            box_title.style.width="100%";
            box_title.style.height="40px";
            box_title.style.lineHeight="40px";
            box_title.style.color=msgbox.color[msgbox.type];
            box_title.style.backgroundColor="#f2f2f2";
            box_title.style.borderBottom="solid 1px #e2e2e2";
            box_title.style.fontWeight="bold";
            box_title.innerText=msgbox.lang=="zh"?msgbox.zh[msgbox.type]:msgbox.type;
            
            var box_cont=document.createElement("div");
            box_cont.style.height="80px";
            box_cont.style.overflow="auto";
            box_cont.style.padding="10px 20px";
            box_cont.style.lineHeight="20px";
            box_cont.style.textAlign="left";
            box_cont.innerText=msg;

            var box_bts=document.createElement("div");
            box_bts.style.padding="10px";
            var box_bt1=document.createElement("button");
            box_bt1.style.backgroundColor=msgbox.color[msgbox.type];
            box_bt1.style.border="0";
            box_bt1.style.width="90px"
            box_bt1.style.height="40px";
            box_bt1.style.color="white";
            box_bt1.style.cursor="pointer";
            box_bt1.innerText="确定";
            box_bt1.addEventListener("click",function(){
                msgbox.CloseBox();
            });
            box_bt1.addEventListener("mouseover",function(){
                this.style.opacity=0.7;
            });
            box_bt1.addEventListener("mouseout",function(){
                this.style.opacity=1;
            });
            box_bts.appendChild(box_bt1);
            if(msgbox.type=="confirm"){
                var box_bt2=document.createElement("button");
                box_bt2.style.border="solid 1px "+msgbox.color[msgbox.type];
                box_bt2.style.backgroundColor="white";
                box_bt2.style.width="90px"
                box_bt2.style.height="40px";
                box_bt2.style.color=msgbox.color[msgbox.type];
                box_bt2.style.cursor="pointer";
                box_bt2.style.marginLeft="20px";
                box_bt2.innerText="取消";
                box_bt2.addEventListener("click",function(){
                    msgbox.callback="";
                    msgbox.CloseBox();
                });
                box_bt2.addEventListener("mouseover",function(){
                    box_bt2.style.backgroundColor="#f2f2f2";
                    this.style.opacity=0.7;
                });
                box_bt2.addEventListener("mouseout",function(){
                    box_bt2.style.backgroundColor="white";
                    this.style.opacity=1;
                });
                box_bts.appendChild(box_bt2);
            }
            
            box.appendChild(box_title);
            box.appendChild(box_cont);
            box.appendChild(box_bts);
        }

        page.appendChild(box);
        document.body.appendChild(page);
    }
}